export const CrudRMixin = {
  data() {
    return {
      currentPage: 1,
      total: 0,
      pager: 1,
      pageSize: 10,
      loadList: true,
      params: {},
      tableData: [],
      pageSizeList: [10, 20, 50, 100, 200],
      layoutConf: 'total, sizes, prev, pager, next, jumper',
      getListFunc: (currentPage, pageSize, params) => {
        return new Promise((resolve, reject) => { })
      },
      delItemFunc: (id) => {
        return new Promise((resolve, reject) => { })
      },
      add_url: '',
      edit_url: (id) => { return '' }
    }
  },
  created() {
    this.refresh_data()
  },
  methods: {
    refresh_data(currentPage) {
      if (!currentPage) currentPage = 0
      this.getListFunc(currentPage, this.pageSize, this.params).then(data => {
        if (data.code === 0) {
          this.tableData = data.payload.list
          this.total = data.payload.total
          this.currentPage = data.payload.currentPage
        } else {
          this.$message.error(data.description)
          this.currentPage = 1
        }
        this.loadList = false
      }).catch(err => {
        console.log(err)
        this.loadList = false
      })
    },
    add() {
      console.log(this.add_url, '点的啥？');
      this.$router.push(this.add_url)
    },
    edit(id) {
      this.$router.push(this.edit_url(id))
    },
    del(id) {
      this.$confirm('确认删除该条数据？', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delItemFunc(id).then(data => {
          if (data.code === 0) {
            this.refresh_data(this.currentPage)
            this.$message.success('删除成功')
          } else {
            this.$message.error(data.description)
          }
        })
      }).catch(() => {
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.refresh_data(0, this.params)
    },
    handleCurrentChange(val) {
      this.refresh_data(val - 1, this.params)
    }
  }
}
